package com.Shiliu.service;

import com.sun.istack.internal.logging.Logger;
import com.Shiliu.dao.BaseDao;
import com.Shiliu.dao.UserDao;
import com.Shiliu.dao.UserDaoImpl;
import com.Shiliu.pojo.User;

import java.sql.Connection;
import java.sql.SQLException;

public class UserServiceImpl  implements userService {
      private  UserDao userDao;//声明接口变量
    private static Logger log=Logger.getLogger(UserServiceImpl.class);
    public  UserServiceImpl() {
        userDao = new UserDaoImpl();//实例化，存放实现类对象
    }



    public User loginImpl(String userCode, String password) {
    Connection connection=null;
    User user=null;
       log.info("进入loginImpl方法");
    try
    {connection = BaseDao.getConnection();
        log.info("走到serviceimpl");
        if(connection==null){
            log.info("conncetion为空");
        }
        user=userDao.getLoginUser(connection,userCode,password);
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }finally{
        BaseDao.release(connection,null,null);
    }
       return user;
    }

    public int  registerImpl(User user)
    {
        Connection connection = null;
        int re = 0;
        try {
            connection = BaseDao.getConnection();
            log.info("进入registerImpl方法");
            re = userDao.add(connection, user);

        }catch (SQLException throwables) {
        } catch (Exception e) {
            e.printStackTrace();
            try {
                System.out.println("rollback==================");
                connection.rollback();//失败就回滚
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }finally{

                BaseDao.release(connection,null,null);
            }

        return re;
    }


    public int modifyImpl(User user, int id) {

        Connection connection = null;
        int re = 0;
        try {
            connection = BaseDao.getConnection();
            log.info("进入modifyImpl方法");
            re = userDao.modify(connection, user,id);

        }catch (SQLException throwables) {
        } catch (Exception e) {
            e.printStackTrace();
            try {
                System.out.println("rollback==================");
                connection.rollback();//失败就回滚
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }finally{

            BaseDao.release(connection,null,null);
        }

        return re;

    }


    public User getMyInfoImpl(Integer id) {
        Connection connection=null;
        User user=null;
        log.info("进入getMyInfoImpl方法");
        try
        {connection = BaseDao.getConnection();
            log.info("走到getMyInfoserviceimpl");
            if(connection==null){
                log.info("conncetion为空");
            }
            user=userDao.getMyInfo(connection,id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            BaseDao.release(connection,null,null);
        }
        return user;
    }


}
